package com.zjh.photoshopstudy.app.video;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;

import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.Model.CommentEntity;
import com.zjh.photoshopstudy.Model.HistoryEntity;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.Utils.SharePreferencesUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class VideoPresent {
    private VideoActivity mActivity;
    private VideoHttp mHttp;

    VideoPresent(VideoActivity mActivity) {
        this.mActivity = mActivity;
        mHttp = new VideoHttp();
    }

    void sendComment(CommentEntity mReplayCommentEntity, VideoEntity mVideoEntity, String text, List<String> uris) {
        CommentEntity commentEntity = new CommentEntity();
        VideoEntity videoEntity = new VideoEntity();
        AppUserEntity userEntity = SharePreferencesUtils.getLoginUser(mActivity);
        if (userEntity == null) {
            Toast.makeText(mActivity, "发送失败，请重新登录", Toast.LENGTH_SHORT).show();
            return;
        }
        if (mReplayCommentEntity != null) {
            AppUserEntity replayUser = mReplayCommentEntity.getAppUserEntity();
            commentEntity.setReplyId((int) (replayUser.getId()));
            commentEntity.setcParentId(1);
            commentEntity.setUserName(replayUser.getuName());
        } else {
            commentEntity.setReplyId((int) mVideoEntity.getvAppUserEntity().getId());
        }

        File[] files = new File[uris.size()];
        List<String> list = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            try (Cursor cursor = mActivity.getContentResolver().query(Uri.parse(uris.get(i)), null, null, null, null)) {
                cursor.moveToNext();
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
                files[i] = new File(path);
                list.add(files[i].getName());
            }
        }


        String s = "查看图片 ";
        int i = 0;
        while (text.contains(s)) {
            String r = "<i>" + list.get(i) + "</i>";
            text = text.replaceFirst(s, r);
        }
        videoEntity.setId(mVideoEntity.getId());
        commentEntity.setcVideoEntity(videoEntity);
        commentEntity.setAppUserEntity(userEntity);
        commentEntity.setcContent(text);
        commentEntity.setCreateTime(new Date());
        mHttp.sendComment(new SendCommentCallBack(), files, commentEntity);
    }

    void Collection(VideoEntity mVideoEntity) {
        AppUserEntity userEntity = SharePreferencesUtils.getLoginUser(mActivity);
        mHttp.doCollection(new CollectionCallBack(), mVideoEntity, (int) userEntity.getId());
    }

    void saveHistory(VideoEntity entity, long current) {
        HistoryEntity historyEntity = new HistoryEntity();
        historyEntity.setCurrent(current);
        historyEntity.setVideoEntity(entity);
        historyEntity.setId(entity.getId());
        SharePreferencesUtils.saveHistory(historyEntity, mActivity);
    }

    void doCancelCollection(VideoEntity mVideoEntity) {
        AppUserEntity userEntity = SharePreferencesUtils.getLoginUser(mActivity);
        mHttp.doCancelCollection(new CancelCollectionCallBack(), mVideoEntity, (int) userEntity.getId());
    }

    boolean IsLoginUser() {
        AppUserEntity user = SharePreferencesUtils.getLoginUser(mActivity);
        return user != null;
    }

    boolean IsCollection(int vid) {
        AppUserEntity user = SharePreferencesUtils.getLoginUser(mActivity);
        if (user != null) {
            Set<Integer> set = SharePreferencesUtils.getCollection(mActivity, (int) user.getId());
            if (set != null) {
                return set.contains(vid);
            }

        }
        return false;
    }

    public void getVideo(long id) {
        mHttp.getVideo(this, id);
    }

    public void getVideoSuccess(VideoEntity videoEntity) {
        mActivity.getVideoSuccess(videoEntity);
    }

    protected class CancelCollectionCallBack {
        void onCancelCollectionSuccess(VideoEntity mVideoEntity, int uid) {
            Set<Integer> set = new HashSet<>();
            set.add((int) mVideoEntity.getId());
            SharePreferencesUtils.deleteCollection(mActivity, uid, set);
            mActivity.onCancelCollectionSuccess();
        }

        public void onCancelCollectionFail() {
            mActivity.onCancelCollectionFail();
        }
    }

    protected class CollectionCallBack {
        void onCollectionSuccess(VideoEntity mVideoEntity, int uid) {
            SharePreferencesUtils.saveCollection(mActivity, uid, (int) mVideoEntity.getId());
            mActivity.onCollectionSuccess();
        }

        public void onCollectionFail() {
            mActivity.onCollectionFail();
        }
    }

    protected class SendCommentCallBack {
        void onSendSuccess(CommentEntity commentEntity) {
            mActivity.onSendCommentSuccess(commentEntity);
        }

        public void onSendFail() {
            mActivity.onSendCommentFail();
        }
    }
}
